////////////////////////////////////////////////////////////////////////////////
//
//  MICROSOFT CORPORATION
//  Copyright (c) Microsoft Corporation.
//  All Rights Reserved.
//
//  NOTICE: Microsoft Confidential. Intended for Internal Use Only.
//
////////////////////////////////////////////////////////////////////////////////

package com.msn.beet.tracking.atlas
{
/*
 * Available methods in Atlas.js:
 *
 * Atlas.Go
 * Atlas.Action
 *
 * Atlas.SetCountry(String):void
 * Atlas.SetLocation(String):void
 * Atlas.SetLanguage(String):void
 * Atlas.SetTimePivot(String):void
*/

import com.msn.beet.logging.Log;
import com.msn.beet.tracking.ISender;
import com.msn.beet.tracking.ITrackAction;
import com.msn.beet.tracking.ITrackModel;

import flash.external.ExternalInterface;

/**
 * Flash interface for Atlas.js.
 *
 */
public class AtlasSenderJS implements ISender
{
    include "../../core/Version.as";

    /**
     * Reference for section and location values if set via track actions,
     * otherwise use setter to control independently of track actions.
     */
    protected var _model:ITrackModel;

    /**
     * Checks for script access, throws Error if not available.
     * @throws Error if script access is disabled.
     */
    public function AtlasSenderJS()
    {
        if (!ExternalInterface.available)
        {
            var error:Error = new Error(
                "Script access disabled. " +
                "Tracking calls cannot be sent to Atlas.js");
            Log.fatal(error);
            throw error;
        }

        // reference for section and location values.
        _model = new AtlasModel();
    }

    /**
     * Calls Go : function(country, project, location, view, unload(
     * Sets base parameters in Atlas.js, no need to call if this is already
     * invoked by javascript.
     * @param country
     * @param baseActionTag
     * @param location
     * @param sendInitialView    Whether Atlas.js should send tracking call
     *                           on page load.
     * @param sendUnloadTrack    Whether Atlas.js should send tracking call
     *                           on page unload.
     * @return
     */
    public function initializeAtlas(country:String, baseActionTag:String,
                                    location:String,
                                    sendPageLoadTrack:Boolean = false,
                                    sendUnloadTrack:Boolean = false):Boolean
    {
        _model.location = location;
        return ExternalInterface.call("Atlas.Go", country, baseActionTag,
                                      location, 0, 1);
    }

     /**
      * Specify a different model in the event that section and location are
      * determined by the
      * application state.
      * @param value
      */
     public function set model(value:ITrackModel):void
    {
        _model = value;
    }

    /**
     * Invokes assemble strategy in TrackAction to get correct string,
     * passes returned value
     * to issueTrack()
     * @param trackAction
     */
    public function send(trackAction:ITrackAction):void
    {
        if (trackAction is IAtlasTrackAction)
        {
            var track:String =
                IAtlasTrackAction(trackAction).parseForAtlasJS(_model);

            if (track != null)
            {
                ExternalInterface.call("Atlas.Action", track);
            }
        }
    }

}

}
